package com.example.tests;

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;
import java.util.Random;
import org.testng.annotations.Test;
import com.example.utils.SortedListOf;

public class GroupModificationTests extends TestBase {
	
	@Test(dataProvider = "randomValidGroupsGenerator")
	public void modifySomeGroup(GroupData group) {
		
		SortedListOf<GroupData> oldList = app.getModel().getGroups();
		    
		Random rnd = new Random();
		int index =  rnd.nextInt(oldList.size()-1);
		
		app.getGroupHelper().modifyGroup(index, group);
		
		SortedListOf<GroupData> newList = app.getModel().getGroups();
		
		assertThat(newList, equalTo(oldList.without(index).withAdded(group)));
			
		if (wantToCheck()) {
			  if ("yes".equals(app.getProperty("check.db"))) {	
				  
				  //SortedListOf<GroupData> mdGroups = app.getModel().getGroups();
				  //SortedListOf<GroupData> dbGroups = app.getHibernateHelper().listGroups();
				  
				  GroupData.assertDbGroupsEqual(app.getModel().getGroups(), app.getHibernateHelper().listGroups());
				  System.out.println("DB verification passed");
			  }
			  if ("yes".equals(app.getProperty("check.ui"))) {
				  
				  SortedListOf<GroupData> mdGroups = GroupData.buildFromGroupList(app.getModel().getGroups());
				  SortedListOf<GroupData> uiGroups = app.getGroupHelper().getUIGroups();
				  
				  assertThat(mdGroups, equalTo(uiGroups));
				  System.out.println("UI verification passed");
			  }
		}
	
	}
	
}
